package com.zl.tby.screen.controller;

import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyJointDefenceQuery;
import com.zl.tby.model.TbyJointDefence;
import com.zl.tby.model.TbyVillageInfo;
import com.zl.tby.model.TbyVillagePerson;
import com.zl.tby.model.TbyWorkDynamics;
import com.zl.tby.screen.enums.TbyCommunityDetailEnum;
import com.zl.tby.screen.service.ITbyCommunityScreenService;
import com.zl.tby.screen.vo.community.TbyCommunityBean;
import com.zl.tby.service.ITbyJointDefenceService;
import com.zl.tby.service.ITbyVillageInfoService;
import com.zl.tby.service.ITbyVillagePersonService;
import com.zl.tby.service.ITbyWorkDynamicsService;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.Valid;
import org.noear.wood.utils.StringUtils;

import java.util.List;

/**
 * 社区共管大屏
 * @author changpeng
 * @version 1.0
 * @since 2024/8/22 15:56
 */
@Valid
@Controller
@Mapping("/community")
public class TbyCommunityScreenController {

    @Inject
    private ITbyCommunityScreenService tbyCommunityScreenService;
    @Inject
    private ITbyJointDefenceService tbyJointDefenceService;
    @Inject
    private ITbyVillageInfoService tbyVillageInfoService;
    @Inject
    private ITbyWorkDynamicsService tbyWorkDynamicsService;
    @Inject
    private ITbyVillagePersonService tbyVillagePersonService;

    /**
     * 社区村概况
     * @return 结果
     */
    @Mapping(value = "/statistic", method = MethodType.GET)
    public R<TbyCommunityBean> statistic() {
        return R.ok(tbyCommunityScreenService.statistic());
    }

    /**
     * 社区帮扶
     * @param createTimeBegin：开始时间
     * @param createTimeEnd：结束时间
     * @return 结果
     */
    @Mapping(value = "/help", method = MethodType.GET)
    public R<List<TbyCommunityBean>> help(String createTimeBegin, String createTimeEnd) {
        return R.ok(this.tbyCommunityScreenService.workDynamicsStatistic(createTimeBegin, createTimeEnd));
    }

    /**
     * 获取详情 (如果是联防联动的话，id为联防联动的类型)
     * @param type: 类型
     * @param id：主键id
     * @return 结果
     */
    @Mapping(value = "/detail", method = MethodType.GET)
    public R<TbyCommunityBean> getDetail(String type, Long id) {
        if (StringUtils.isNotEmpty(type) && type.equals(TbyCommunityDetailEnum.VILLAGE.toString())) {
            if (id != null) {
                TbyVillageInfo detail = this.tbyVillageInfoService.get(id);
                if (detail != null) {
                    TbyCommunityBean entity = new TbyCommunityBean();
                    entity.setBusinessId(detail.getId());
                    entity.setTitle(detail.getVillageName());
                    entity.setContent(detail.getContent());
                    return R.ok(entity);
                }
            }
        } else if (StringUtils.isNotEmpty(type) && type.equals(TbyCommunityDetailEnum.JOINTDEFENCE.toString())) {
            TbyJointDefenceQuery query = new TbyJointDefenceQuery();
            query.setType(id.intValue());
            query.setPublish(1);
            List<TbyJointDefence> dataList = this.tbyJointDefenceService.list(query);
            if (dataList != null && dataList.size() > 0) {
                TbyJointDefence defence = dataList.get(0);
                if (defence != null) {
                    TbyCommunityBean entity = new TbyCommunityBean();
                    entity.setBusinessId(defence.getId());
                    entity.setTitle(defence.getTitle());
                    entity.setContent(defence.getContent());
                    return R.ok(entity);
                }
            }
        } else if (StringUtils.isNotEmpty(type) && type.equals(TbyCommunityDetailEnum.WORKDYNAMICS.toString())) {
            TbyWorkDynamics dynamics = this.tbyWorkDynamicsService.get(id);
            if (dynamics != null) {
                TbyCommunityBean entity = new TbyCommunityBean();
                entity.setBusinessId(dynamics.getId());
                entity.setTitle(dynamics.getTitle());
                entity.setContent(dynamics.getContent());
                return R.ok(entity);
            }
        }
        return R.ok(null);
    }

    /**
     * 根据年份范围获取年份的人员总数
     * @param startYear
     * @param endYear
     * @return
     */
    @Mapping(value = "/village_person_count_year", method = MethodType.GET)
    public R<List<TbyVillagePerson>> village_person_list(String startYear, String endYear, Integer villageId) {
        return R.ok(this.tbyVillagePersonService.personCountByYear(startYear, endYear, villageId));
    }

}
